{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "使用vscode时,需要加入pyd的路径,加入方式如下:\n",
    "- 将pyd放入到.py同级目录即可\n",
    "- 使用sys.path.append(f\"{os.getcwd()}/../cpp/cmake-build-release/lib\")\n",
    "- 也可以尝试去增加搜索路径,本例未验证"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "1740a056ee51539a"
  },
  {
   "cell_type": "markdown",
   "source": [
    "使用jetbrains系列ide时，在interpreter中加入pyd所在路径即可。\n",
    "当更新了pyd后，需要更新下存根，才能正确的智能补全，更新方式：\n",
    "1、将python_stubs(C:\\Users\\ZrNotebook\\AppData\\Local\\JetBrains\\PyCharm2023.3\\python_stubs)文件夹内的非cache文件夹删除;\n",
    "\n",
    "2、更新存根，这里有如下几种方式：\n",
    "- 重启ide\n",
    "- 在interpreter中点击更新图标\n",
    "- 焦点在文件内时,双击shift,输入Rescan并使用"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "781371e8e48fedeb"
  },
  {
   "cell_type": "markdown",
   "source": [
    "先准备好环境，将库所在的文件夹设置为搜索路径"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "1d688c1a47285b95"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "import os\n",
    "import sys\n",
    "\n",
    "# 加入Qt的dll路径\n",
    "os.add_dll_directory(\"C:/Qt/5.15.2/msvc2019_64/bin\")\n",
    "# 需要先将C++编译好的库的路径加入到python的包检索路径\n",
    "sys.path.append(f\"{os.getcwd()}/../cpp/cmake-build-release/bin\")"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "6692453b2d7aef1f",
   "execution_count": null
  },
  {
   "cell_type": "markdown",
   "source": [
    "导入目标库后进行测试，先测试下最简单的函数调用"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "b55bcc26291e3f14"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "# 导入测试库\n",
    "import Pybind11Lib\n",
    "\n",
    "# 调用最简单的函数\n",
    "Pybind11Lib.add(1, 2)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "4a1b9ebe3a8e78bd",
   "execution_count": null
  },
  {
   "cell_type": "markdown",
   "source": [
    "测试下lambda函数"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "ed48ff9fb05078d2"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "Pybind11Lib.add_lambda(2, 3)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "5cf7bb4e38d3941c",
   "execution_count": null
  },
  {
   "cell_type": "markdown",
   "source": [
    "测试回调函数"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "5a60d6a2b3949ae7"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "# 定义回调函数\n",
    "def myCallFunc(arg0, arg1, myStr):\n",
    "    print(f\"hello: {myStr}\")\n",
    "    return arg0 * arg1\n",
    "\n",
    "\n",
    "Pybind11Lib.add_callback(1, 2, \"world\", myCallFunc)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "4d87bf2143c4f167",
   "execution_count": null
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "# 使用类\n",
    "myClass = Pybind11Lib.MyClass()\n",
    "myClass.add(1, 2)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "5cbb8d509ad5934b",
   "execution_count": null
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "# 使用指针\n",
    "v = Pybind11Lib.addPtr(1, 2)\n",
    "Pybind11Lib.usePtr(v)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "f3f13dc0d7001fc6",
   "execution_count": null
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "# 使用字符串\n",
    "Pybind11Lib.theStr(\"test\")"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "f6ee43dd797f0111",
   "execution_count": null
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "# 使用依赖的其他库的函数\n",
    "Pybind11Lib.sub(10, 3)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "1d3d356739c86697",
   "execution_count": null
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
